use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .create_table(
                Table::create()
                    .table(Project::Table)
                    .if_not_exists()
                    .col( ColumnDef::new(Project::Id).integer().not_null().auto_increment().primary_key().comment("id") )
                    .col( ColumnDef::new(Project::Title).string_len(30).not_null().unique_key().comment("项目名称") )
                    .col( ColumnDef::new(Project::Folder).string_len(35).comment("后端开发目录") )
                    .col( ColumnDef::new(Project::FolderFront).string_len(35).comment("前端开发目录") )
                    .col( ColumnDef::new(Project::Database).string_len(20).not_null().comment("数据库名") )

                    .col( ColumnDef::new(Project::Url).string_len(60).comment("前端访问地址") )
                    .col( ColumnDef::new(Project::ApiUrl).string_len(60).comment("api") )
                    .col( ColumnDef::new(Project::SqlUrl).string_len(60).comment("数据库") )
                    .col( ColumnDef::new(Project::MgUrl).string_len(60).comment("Mongodb") )
                    .col( ColumnDef::new(Project::RedisUrl).string_len(60).comment("Redis") )
                    .col( ColumnDef::new(Project::Backup).string_len(60).comment("备份目录") )
                    .col( ColumnDef::new(Project::Enabled).tiny_unsigned().comment("启用") )
                    // 固定字段
                    .col( ColumnDef::new(Project::CreateUser).integer().not_null().comment("创建人") )
                    .col( ColumnDef::new(Project::UpdateUser).integer().comment("修改人") )
                    .col( ColumnDef::new(Project::CreateTime).timestamp().default(Expr::current_timestamp()).comment("创建时间") )
                    .col( ColumnDef::new(Project::UpdateTime).timestamp().default(Expr::current_timestamp()).extra("on update current_timestamp").comment("更新时间") )
                    .to_owned(),
            )
            .await
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager.drop_table(Table::drop().table(Project::Table).to_owned()).await
    }
}

#[derive(DeriveIden)]
enum Project {
    Table,
    Id,
    Title,
    Folder,
    FolderFront,
    Database,
    Url,
    ApiUrl,
    SqlUrl,
    MgUrl,
    RedisUrl,
    Backup,

    Enabled,

    CreateUser,
    UpdateUser,
    CreateTime,
    UpdateTime,
}